# import scrapy
#
#
# class PubgSpider(scrapy.Spider):
#     name = "pubg"
#     # allowed_domains = ["yyjhbg.com"]
#     start_urls = ["https://pubg.com/zh-cn/game-info/weapons/ar"]
#
#     def parse(self, response):
#         for game in response.xpath("//div[@class='weapons__header']//ul/li/a/span/text()").getall():
#
#             # img=response.xpath('')
#             # print(response.xpath("//div[@class='weapons__header']//ul/li/a/span/text()").extract())
#             for url in response.xpath("//div[@class='weapons__header']//ul/li/a/@href").extract():
#                 yield response.follow(url,callback=self.parse_data,cb_kwargs=dict(game=game))
#
#     def parse_data(self, response, game):
#         print(f'正在爬取{game}')
#         name = response.xpath(
#             "//ul[contains(@class, 'weapons-list__list') and contains(@class, 'weapons-list')]//span[@class='weapons-card__image-area']/span/text()").getall()
#         print(name)
import scrapy

class PubgSpider(scrapy.Spider):
    name = "pubg"
    start_urls = ["https://pubg.com/zh-cn/game-info/weapons/ar"]

    def parse(self, response):
        # 获取每个武器分类的链接和名称
        for item in response.xpath("//div[@class='weapons__header']//ul/li"):
            game = item.xpath("./a/span/text()").get()
            url = item.xpath("./a/@href").get()
            if game and url:
                yield response.follow(url, callback=self.parse_data, cb_kwargs=dict(game=game.strip()))

    def parse_data(self, response, game):
        # 提取武器名称
        names = response.xpath("//ul[contains(@class, 'weapons-list__list')]//span[@class='weapons-card__image-area']/span/text()").getall()
        for name in names:
            yield {
                'category': game,
                'weapon_name': name.strip()
            }